<?php

$perl_tpl ="
my  \$last_position = -1;
my  @music = ();
my  \$jingle;

sub  ices_init  {
    print \"Perl subsystem Initializing:\\n\";
    \$jingle = 0;
    return  1;
}

sub  ices_shutdown  {
    print \"Perl subsystem shutting down:\\n\";
}

sub  ices_get_next  {
    print \"Perl subsystem quering for new track:\\n\";
    if   (\$jingle < 3)  {
        \$jingle ++;
        @music = `/bin/ls -1 ".$gl["ftp_dir"]."/".$input["login"]."/music/*.mp3 2>/dev/null`;
        if   (@music == NULL)  {
            @music = `/bin/ls -1 ".$gl["ftp_dir"]."/".$input["login"]."/jingls/*.mp3 2>/dev/null`;
            if   (@music == NULL)  {
                @music=\"/users/intro.mp3\";
            }
        }
    }
    else  {
        \$jingle = 0;
        @music = `/bin/ls -1 ".$gl["ftp_dir"]."/".$input["login"]."/music/*.mp3 2>/dev/null`;
        if   (@music == NULL)  {
            @music = `/bin/ls -10 ".$gl["ftp_dir"]."/".$input["login"]."/jingls/*.mp3 2>/dev/null`;
            if   (@music == NULL)  {
                @music = \"/users/intro.mp3\";
            }
        }
    }

    my  \$n_try = 0;
    my  \$num = scalar( @music );
    my  \$play;

    while  (\$n_try ++ < \$num)  {
        \$play = int( rand( \$num ) );
        # print  \"           last_played  = \", \$last_played, \"\\n\";
        # print  \"           current_play = \", \$play, \"\\n\";
        next   if   (\$last_played == \$play);
        chomp  \$music[\$play];
        last   if   (\$last_played != \$play);
    }

    \$last_played = \$play   if  \$jingle > 0;

    return  \$music[\$play];
}

return 1;";

?>
